﻿using DataTransfer.SDK.Models;
using MiniFox.Data;
using MiniFox.Platform.Applications.Auth;
using MiniFox.Utility;
using System.Data;
using DataSourceFactory = MiniFox.Platform.DataSourceFactory;

namespace DataTransfer.Core.Services
{
    /// <summary>
    /// 默认数据加载器，从数据库命令工厂提取
    /// </summary>
    public class DefaultDataLoader : DataLoader
    {
        DataSourceFactory _factory;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="exporter"></param>
        /// <param name="authStateManagement"></param>
        public DefaultDataLoader(IExcelExporter exporter, IAuthStateManagement authStateManagement) : base(exporter, authStateManagement)
        {
            Exporter = exporter;
        }
        /// <summary>
        /// 
        /// </summary>
        protected new IExcelExporter Exporter
        {
            get;
        }
        /// <summary>
        /// 
        /// </summary>
        protected DataSourceFactory DataSourceFactory
        {
            get
            {
                _factory ??= GetComponent<DataSourceFactory>(AuthStateManagement);
                return _factory;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="queryContent"></param>
        /// <returns></returns>
        public override object ExecuteLoad(object queryContent)
        {
            DataSet ds = new(Exporter.Name);
            IDictionary<string, object> dict = queryContent.ToDictionary();
            foreach (IExcelExportSheet sheet in Exporter.Sheets)
            {
                object parameters = null;
                if (dict != null && dict.TryGetValue(sheet.QueryName, out object? value))
                    parameters = value;
                DataTable dataTable = LoadTable(sheet, parameters);
                ds.Tables.Add(dataTable);
            }
            return ds;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="parameters"></param>
        protected virtual DataTable LoadTable(IExcelExportSheet sheet, object parameters)
        {
            var command = DataSourceFactory.GetDataCommand(sheet.QueryName);

            command.CommandExecuting += Command_CommandExecuting;
            command.CommandExecuted += Command_CommandExecuted;
            command.OnError += Command_OnError;
            DataTable dataTable = new(sheet.SheetName);
            int i = 0;
            foreach (IExcelExportSheetField field in sheet.Fields)
            {
                var column = dataTable.Columns.Add(field.FieldName, field.Type);
                column.Caption = field.DisplayName;
                column.SetOrdinal(i++);
            }
            command.FillDataTable(dataTable, parameters);
            return dataTable;
        }
        private void Command_OnError(object? sender, CommandErrorEventArgs e)
        {
            var content = new
            {
                ExporterName = Name,
                e.ConnectionString,
                e.CommandText,
                e.Parameters,
                e.Error.Message
            };
            WriteErrorLog(content.ToJsonString(), e.Error);
        }

        private void Command_CommandExecuted(object? sender, CommandEventArgs e)
        {
        }

        private void Command_CommandExecuting(object? sender, CommandEventArgs e)
        {
        }
    }
}
